package com.example.appkcsj.slice;

import com.example.appkcsj.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
    private final static HiLogLabel HI_LOG_LABEL=
            new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbilitySlice");
    private TextField username;
    private TextField password;
    private TextField tf;
    Button btu3;
    Button btu4;
    Text t;
    private DBUtils dbUtils;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //找到按钮
        dbUtils=new DBUtils(MainAbilitySlice.this);
        username= (TextField) findComponentById(ResourceTable.Id_field1);
        password= (TextField) findComponentById(ResourceTable.Id_field2);
        btu3= (Button) findComponentById(ResourceTable.Id_but1);
        btu4= (Button) findComponentById(ResourceTable.Id_but2);
        btu3.setClickedListener(this);
        btu4.setClickedListener(this);
        t=(Text) findComponentById(ResourceTable.Id_text);
        //给按钮添加点击事件
        t.setClickedListener(this);
        if(intent!=null){
            String name=intent.getStringParam("username1");
            String pass1=intent.getStringParam("password1");
            String pass2=intent.getStringParam("password2");
            Student student=new Student(name,pass1,pass2);
            dbUtils.insertStudent(student);
        }
        btu3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Student student=dbUtils.queryOneStudent();
                if ("".equals(username.getText()) || "".equals(password.getText())) {
                    ToastDialog td = new ToastDialog(MainAbilitySlice.this);
                    td.setText("请输入用户名及密码！");
                    td.show();
                } else {
                    if(student.getUsername1().equals(username.getText())) {
                        if (student.getPassword1().equals(password.getText())) {
                            Intent m = new Intent();
                            Operation operation = new Intent.OperationBuilder()
                                    .withDeviceId("")
                                    .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                    .withAbilityName("com.example.appkcsj.thirdAbility")//要跳转的包名
                                    .build();//将上面的信息打包
                            m.setOperation(operation);
                            startAbility(m);
                            ToastDialog td = new ToastDialog(MainAbilitySlice.this);
                            td.setText("登录成功！");
                            td.show();
                        }
                        else{
                            ToastDialog td = new ToastDialog(MainAbilitySlice.this);
                            td.setText("密码输入错误！");
                            td.show();
                        }
                    }
                    else{
                        if (student.getPassword1().equals(password.getText())) {
                            ToastDialog td = new ToastDialog(MainAbilitySlice.this);
                            td.setText("用户名输入错误！");
                            td.show();
                        } else{
                            ToastDialog td = new ToastDialog(MainAbilitySlice.this);
                            td.setText("用户名及密码输入错误!");
                            td.show();
                        }
                    }
                }

            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    public void onClick(Component component) {

        if(component == btu4){
            Intent n=new Intent();
            Operation operation=new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.appkcsj")//跳转到哪个应用
                    .withAbilityName("com.example.appkcsj.secondAbility")//要跳转的包名
                    .build();//将上面的信息打包
            n.setOperation(operation);
            startAbility(n);
        }
        if (component == t) {
            //点击btu才跳转
            Intent i = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.appkcsj")
                    .withAbilityName("com.example.appkcsj.fifthAbility")
                    .build();
            i.setOperation(operation);
            startAbility(i);
        }

    }

}
